#include <bits/stdc++.h>

using namespace std;
using ll=long long;

template<typename T=int>
inline T read() {
    T x;
    cin >> x;
    return x;
}

#define endl '\n'

struct BTree {
    char root;
    BTree *left, *right;

    BTree(char root, BTree *left = nullptr, BTree *right = nullptr) : root(root), left(left), right(right) {}

    BTree(const string pre, const string mid) {
        if (mid.empty())return;
        if (mid.size() == 1) {
            root = mid[0];
            left = right = nullptr;
            return;
        }
        string lmid, lpre, rmid, rpre;
        auto pos = mid.find(pre[0]);
        lmid = mid.substr(0, pos);
        rmid = mid.substr(pos + 1);
        for (int i = 1; i < pre.length(); i++) {
            if (lmid.find(pre[i]) != string::npos) {
                lpre.push_back(pre[i]);
            }
            if (rmid.find(pre[i]) != string::npos) {
                rpre.push_back(pre[i]);
            }
        }
        left = lmid.empty() ? nullptr : new BTree(lpre, lmid);
        right = rmid.empty() ? nullptr : new BTree(rpre, rmid);
        root = pre[0];
    }


    void mid_visit() {
        if (left != nullptr) {
            cout << "(";
            left->mid_visit();
            cout << ")";
        }
        cout << root;
        if (right != nullptr) {
            cout << "(";
            right->mid_visit();
            cout << ")";
        }
    }

};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T = read();
    for (string mid, pre; T-- && cin >> pre >> mid;) {
        BTree btree(pre, mid);
        btree.mid_visit();
        cout << endl;
    }
    return 0;
}
